package pers.vic.xqd.framerwork;


import com.sun.org.apache.bcel.internal.generic.RETURN;
import pers.vic.xqd.protocol.http.HttpClient;
import pers.vic.xqd.protocol.http.HttpProtocol;
import pers.vic.xqd.provider.api.HelloService;
import pers.vic.xqd.register.RemoteMapRegister;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 描述: 代理类工厂
 * @author Vic.xu
 * @date 2022-02-09 11:29
 */
public class ProxyFactory {

    public static <T> T getProxy(Class interfaceClass){
        return (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Protocol httpClient = ProtocolFactory.getProtocol();
                Invocation invocation = new Invocation(interfaceClass.getName(), method.getName(), method.getParameterTypes(), args);
                Url url = RemoteMapRegister.random(interfaceClass.getName());
                String result = httpClient.send(url, invocation);
                return result;
            }
        });

    }
}
